﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using ERPStore.Extensions;

namespace ERPStore.Exporter.Providers
{
	public class CriteoExport : ExportBase
	{
		private XDocument m_Document;

		public CriteoExport() : base()
		{
		}

		public override void InitializeSettings(Models.Settings settings)
		{
			settings.AllowMissingDefaultImage = false;
			settings.AllowNotSellable = false;
			settings.AllowNullLongDescription = true;
			settings.AllowNullShortDescription = true;
			settings.AllowStockEmpty = true;
			settings.AllowZeroSalePrice = false;
			settings.FileName = "criteo.xml";
		}

		public override string Name
		{
			get { return "Criteo"; }
		}

		public override string LeadSourceId
		{
			get { return "F1089D30-12F0-4473-BF3D-78A3FBAAF905"; }
		}

		public override void CreateDocument()
		{
			m_Document = new XDocument(new XDeclaration("1.0", "UTF-8", null));
			var root = new XElement("products");
			m_Document.Add(root);
		}

		public override void AddElementToDocument(ERPStore.Models.Product product, ERPStore.Models.ProductStockInfo productStock)
		{
			var id = product.Code;
			var name = product.PageTitle ?? product.Title;
			var smallimage = string.Format("http://{0}{1}", GlobalConfiguration.Configuration.Settings.DefaultUrl, string.Format(product.DefaultImage.Url, 40, 40));
			var bigimage = string.Format("http://{0}{1}", GlobalConfiguration.Configuration.Settings.DefaultUrl, string.Format(product.DefaultImage.Url, 0, 0));
			var producturl = BuildEncryptedProductUrl(LeadSource, product);
			var description = product.ShortDescription ?? product.LongDescription ?? product.PageTitle ?? product.Title;
			var price = string.Format("{0:F2}", product.BestPrice.ValueWithTax).Replace(",",".");
			var instock = (productStock != null && productStock.AvailableStock > 0) ? "1" : "0";

			var element = new XElement("product");
			element.Add(new XAttribute("id", id));
			element.Add(new XElement("name", new XCData(name)));
			element.Add(new XElement("smallimage", smallimage));
			element.Add(new XElement("bigimage", bigimage));
			element.Add(new XElement("producturl", producturl));
			element.Add(new XElement("description", new XCData(description)));
			element.Add(new XElement("price", price));
			if (product.StrikedPrice != null
				&& product.StrikedPrice.Value > product.BestPrice.Value)
			{
				var retailprice = string.Format("{0:F2}", product.StrikedPrice.ValueWithTax).Replace(",", ".");
				element.Add(new XElement("retailprice", retailprice));
			}
			if (product.Category != null)
			{
				element.Add(new XElement("categorieid1", product.Category.GetRootProductCategory().Name));
			}
			element.Add(new XElement("recommendable", "1"));
			element.Add(new XElement("instock", instock));

			m_Document.Root.Add(element);
		}

		public override void SaveDocument()
		{
			var xmlWriterSettings = new XmlWriterSettings()
			{
				CheckCharacters = false,
				Indent = true,
			};
			var path = Configuration.ConfigurationSettings.AppSettings["exportPath"];
			var fileName = System.IO.Path.Combine(path, Settings.FileName);

			using (var xmlWriter = XmlWriter.Create(fileName, xmlWriterSettings))
			{
				m_Document.Save(xmlWriter);
				xmlWriter.Flush();
				xmlWriter.Close();
			}
		}

		public override void Dispose()
		{
			m_Document = null;
		}

	}
}
